﻿/*****************************************************
*File Name: HiSunCache.cs								
*Writer: 周燕龙		
*Ver:1.0
*Created Date: 2009-06-18						
*Description: 缓存类，实现缓存的存储、清除、指定缓存时间
 *调用：
 *增加
 *if (null == HiSunCache.Get("缓存名称"))
     HiSunCache.Insert("缓存名称", DataTable);
 * 移除
 *if (null != HiSunCache.Get("缓存名称"))
     HiSunCache.Remove("缓存名称");
 * 清除所有
 *  HiSunCache.Clear();
												
*****************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections;
using System.Text.RegularExpressions;

namespace Hisuntech.SystemFramework.Web
{
    public class HiSunCache
    {
        private HiSunCache() { }
        public static readonly int DayFactor = 17280;
        public static readonly int HourFactor = 720;
        public static readonly int MinuteFactor = 12;

        private static readonly Cache _cache;

        private static int Factor = 5;

        static HiSunCache()
        {
            HttpContext content = HttpContext.Current;
            if (content != null)
                _cache = content.Cache;
            else
                _cache = HttpRuntime.Cache;
        }
        /// <summary>
        /// 清除所有catch
        /// </summary>
        public static void Clear()
        {
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
                _cache.Remove(CacheEnum.Key.ToString());

        }
        public static void RemoveByPattern(string pattern)
        {
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
            while (CacheEnum.MoveNext())
            {
                if (regex.IsMatch(CacheEnum.Key.ToString()))
                    _cache.Remove(CacheEnum.Key.ToString());
            }
        }
        /// <summary>
        /// 从catch中移除指定的键
        /// </summary>
        public static void Remove(string key)
        {
            _cache.Remove(key);
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="obj">存储数据</param>
        public static void Insert(string key, object obj)
        {
            Insert(key, obj, null, MinuteFactor * 10);
        }
        public static void InsertEx(string key, object obj)
        {

            Insert("HiSun" + key, obj);
        }
        public static void Insert(string key, object obj, CacheDependency dep)
        {
            Insert(key, obj, dep, HourFactor * 12);
        }

        public static void Insert(string key, object obj, int seconds)
        {
            Insert(key, obj, null, seconds);
        }

        public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
        {
            Insert(key, obj, null, seconds, priority);
        }

        /// <summary>
        /// 重载添加缓存
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="obj">存储数据</param>
        /// <param name="dep"></param>
        /// <param name="seconds">存储时间</param>
        public static void Insert(string key, object obj, CacheDependency dep, int seconds)
        {
            Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
        }

        /// <summary>
        /// 重载添加缓存
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="obj">存储数据</param>
        /// <param name="dep"></param>
        /// <param name="seconds">存储时间</param>
        /// <param name="priority">优先级</param>
        public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(Factor * seconds), TimeSpan.Zero, priority, null);
            }

        }

        public static void MicroInsert(string key, object obj, int secondFactor)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, null, DateTime.Now.AddSeconds(Factor * secondFactor), TimeSpan.Zero);
            }
        }

        /// <summary>
        /// 将键值插入catch，并保持最大时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="obj"></param>
        public static void Max(string key, object obj)
        {
            Max(key, obj, null);
        }

        public static void Max(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                _cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
            }
        }

        /// <summary>
        /// 根据键得到值
        /// </summary>
        public static object Get(string key)
        {
            return _cache[key];
        }
        public static object GetEx(string key)
        {
            return _cache["HiSun" + key];
        }
        internal static void ReSetFactor(int cacheFactor)
        {
            Factor = cacheFactor;
        }
    }
}
